<script setup lang="ts">
import { createPayUrlAPI } from '@/services'
import { showToast } from 'vant'
import { ref } from 'vue'
const props = defineProps<{
  payCallback: string //支付完毕后的回跳地址
  show: boolean //弹层显示隐藏
  orderId: string //订单ID
  money: number //实付款
  handelClose?: () => void //关闭操作
}>()

// 支付方式支付按钮
const payMethod = ref()
const FastPay = async () => {
  // 判断是否选择了支付方式
  if (payMethod.value === undefined) {
    return showToast('请选择支付方式')
  }
  // 调用接口
  const res = await createPayUrlAPI({
    paymentMethod: payMethod.value, //支付方式
    orderId: props.orderId, //订单ID
    payCallback: import.meta.env.VITE_APP_CALLBACK + props.payCallback //支付完成后的放回地址
  })
  // 跳转到支付页面
  window.location.href = res.data.payUrl
}
</script>

<template>
  <!-- 抽屉组件，选择支付方式 -->
  <!-- :close-on-popstate="false"  表示回退时不关闭抽屉组件 -->
  <!-- :closeable="false" 把关闭图标去掉 -->

  <!-- v-model:show="show",因为这里是组件，不能直接关闭，双向绑定实行不通
:show="show" @update:show="show=$event" -->
  <van-action-sheet
    :before-close="handelClose"
    :closeable="false"
    :show="show"
    @update:show="$emit('uodate:show', $event)"
    title="选择支付方式"
    :close-on-popstate="false"
  >
    <div class="pay-type">
      <p class="amount">￥{{ money }}</p>
      <van-cell-group>
        <van-cell title="微信支付">
          <template #icon><CpIcons name="consult-wechat" /></template>
          <template #extra
            ><van-checkbox @click="payMethod = 0" :checked="payMethod === 0"
          /></template>
        </van-cell>
        <van-cell title="支付宝支付">
          <template #icon><CpIcons name="consult-alipay" /></template>
          <template #extra
            ><van-checkbox @click="payMethod = 1" :checked="payMethod === 1"
          /></template>
        </van-cell>
      </van-cell-group>
      <div class="btn">
        <van-button type="primary" @click="FastPay" round block
          >立即支付</van-button
        >
      </div>
    </div>
  </van-action-sheet>
</template>

<style scoped lang="scss">
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>
